package com.ty.day3.home;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import static java.lang.Thread.sleep;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/24 17:17
 */
public class Task1 {
    public static void main(String[] args) throws InterruptedException {
        //写一个倒计时程序，显示举例2024.1.1 还有多少天，小时，分，秒。
        Calendar c = Calendar.getInstance();
        c.set(2024, 0, 1, 0, 0, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("距离" + sdf.format(c.getTime()) + "剩下：");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                long l, day, hour, minute;
                Date date = new Date();
                //一共剩余秒
                l = (c.getTime().getTime() - date.getTime()) / 1000;
                //天数
                day = l / (60 * 60 * 24);
                //除去天数剩下时间
                l = l - day * 60 * 60 * 24;
                hour = l / (60 * 60);
                //去掉小时后剩下的时间
                l = l - hour * 60 * 60;
                minute = l / 60;
                //剩下的秒
                l = l - minute * 60;
                System.out.println(day + "天," + hour + "小时" + minute + "分钟" + l + "秒");
            }
        }, 1, 1000);
    }
}
